//app.js
import WxValidate from 'utils/WxValidate';
import { apiBaseUrl } from 'utils/api';
App({
  wxValidate: (rules, messages) => new WxValidate(rules, messages),
  onLaunch: function () {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter()
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    wx.login({
      success: function (login) {
        wx.getUserInfo({
          withCredentials: true,
          success: function (userInfo) {
            let requestData = {
              code: login.code,
              iv: userInfo.iv,
              ed: userInfo.encryptedData,
              grant_type: 'wechat_small',
              client_id: 1,
              client_secret: 'DzU6FHiNwL5IIp4U2yZrfxVqIoScWHayPWrvSllZ'
            };
            wx.request({
              url: `${apiBaseUrl}oauth/token`,
              method: 'POST',
              data: requestData,
              success: function (res) {
                wx.setStorageSync('ak', res.data.access_token);
                wx.setStorageSync('rk', res.data.refresh_token);
                wx.setStorageSync('aktime', res.data.expires_in);
              },
              fail: function (res) {
                console.log(res.errMsg)
              }
            })

          }
        })
      }
    })
  }
})